local kaifeng = fk.CreateSkill {
  name = "inker_kaifeng",
  tags = { Skill.Compulsory }
}

kaifeng:addEffect("active", {
  anim_type = "offensive",
  prompt = function(self, player, selected_cards, selected_targets)
    return "#inker_kaifeng:::" .. (player:usedSkillTimes(kaifeng.name, player.HistoryPhase) + 1)
  end,
  can_use = function(self, player)
    return table.find(Fk:currentRoom().alive_players, function(p) return player:canPindian(p) end)
  end,
  card_filter = Util.FalseFunc,
  target_num = 1,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and player:canPindian(to_select)
  end,
  on_use = function (self, room, effect)
    local player = effect.from
    local pindian = player:pindian(effect.tos, kaifeng.name)
    for _, result in pairs(pindian.results) do
      if result.winner ~= player then
        room:invalidateSkill(player, kaifeng.name, "-phase", kaifeng.name)

        local count = player:usedSkillTimes(kaifeng.name, player.HistoryPhase)
        for _ = 1, count, 1 do
          local to = room:askToChoosePlayers(player, {
            targets = room.alive_players,
            min_num = 1,
            max_num = 1,
            prompt = "#inker_kaifeng-choose",
            skill_name = kaifeng.name,
            cancelable = false,
          })[1]
          if not to then return end
          local recasted = room:askToCards(to, {
            min_num = 2,
            max_num = 2,
            pattern = ".|.|.|.|.|basic",
            skill_name = kaifeng.name,
            prompt = "#inker_kaifeng-recast:" .. player.id,
          })
          if #recasted == 2 then
            room:recastCard(recasted, to, kaifeng.name)
          else
            local duel = Fk:cloneCard("duel")
            if table.contains(duel:getAvailableTargets(player), to) then
              local use = {
                from = player,
                tos = { to },
                card = duel,
                skill_name = kaifeng.name,
              }
              ---@cast use UseCardData
              room:useCard(use)
              if use.damageDealt then
                for p, _ in pairs(use.damageDealt) do
                  if p:isAlive() then
                    local draws = count - p:getHandcardNum()
                    if draws > 0 then
                      p:drawCards(draws, kaifeng.name)
                    end
                  end
                end
              end
            end
          end
          if player.dead then break end
        end
        return
      end
    end
  end
})

Fk:loadTranslationTable{
  ["inker_kaifeng"] = "忾锋",
  [":inker_kaifeng"] = "出牌阶段，你可拼点：若你没赢，本阶段此技能失效，然后你重复执行X次（X为本阶段此技能发动次数）：令一名角色选择是否重铸两张基本牌，若其不重铸，你视为对其使用【决斗】，受伤角色将手牌数摸至X张。",
  ["#inker_kaifeng"] = "忾锋：与一名角色拼点：若你没赢，重复执行%arg次令人选择重铸或视为你对其使用决斗",

  ["#inker_kaifeng-choose"] = "忾锋：令一名角色选择是否重铸两张基本牌，若其不重铸，你视为对其使用【决斗】",
  ["#inker_kaifeng-recast"] = "忾锋：请重铸两张基本牌，若不重铸，%src视为对你使用【决斗】",

  ["$inker_kaifeng1"] = "武圣之名，威震华夏！",
  ["$inker_kaifeng2"] = "青龙既出，贼将授首！",
}

return kaifeng